package com.wei.czz.framework.index.controller;

import com.wei.czz.common.annotation.Resubmit;
import com.wei.czz.common.constant.Constant;
import com.wei.czz.common.dto.index.login.LoginDto;
import com.wei.czz.common.enums.ResultEnum;
import com.wei.czz.common.enums.admin.user.RequestChannelEnum;
import com.wei.czz.common.tool.Result;
import com.wei.czz.common.utils.ContextUtils;
import com.wei.czz.common.utils.RegexUtils;
import com.wei.czz.common.vo.index.sign.RegisterVo;
import com.wei.czz.framework.index.manager.UserRegisterManager;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2021-02-26 15:56:00
 * className: RegisterController 普通用户注册操作请求接口类
 * version: 1.0
 * description:
 */
@RestController
@RequestMapping("index/user")
@AllArgsConstructor
public class RegisterController {

    private static final Logger log = LoggerFactory.getLogger(RegisterController.class);

    private UserRegisterManager userRegisterManager;

    /**
     * 用户注册接口
     * <p>
     * 调用方：
     *  1、浏览器-用户注册页面调用
     * <p>
     * 此接口会校验是否重复提交
     * @param registerVo 用户注册参数对象
     * @return 请求结果
     */
    @Resubmit(spEL = "'registerUser' + #requestIp", message = "用户注册")
    @PostMapping("/register")
    public Result<LoginDto> registerUser(@Validated @RequestBody RegisterVo registerVo) {
        log.info("开始用户注册业务。{}", registerVo);

        if (StringUtils.isAllBlank(registerVo.getPhone(), registerVo.getEmail())) {
            log.info("用户主键，手机号和邮箱号均为空");
            return Result.paramFail();
        }

        // 手机号正则校验
        if (StringUtils.isNotBlank(registerVo.getPhone())) {
            if (!RegexUtils.isPhone(registerVo.getPhone())) {
                log.info("普通用户注册接口，注册手机号格式错误");
                return Result.getResult(ResultEnum.REQUEST_FAIL.getCode(), "注册手机号格式错误！");
            }
            if (StringUtils.isBlank(registerVo.getPhoneCode())) {
                log.info("用户注册接口，短信验证码参数错误");
                return Result.paramFail();
            }
        }
        // 邮箱号正则校验
        if (StringUtils.isNotBlank(registerVo.getEmail())) {
            if (!RegexUtils.isEmail(registerVo.getEmail())) {
                log.info("普通用户注册接口，注册邮箱号格式错误");
                return Result.getResult(ResultEnum.REQUEST_FAIL.getCode(), "注册邮箱号格式错误！");
            }
            if (StringUtils.isBlank(registerVo.getEmailCode())) {
                log.info("用户注册接口，邮箱验证码参数错误");
                return Result.paramFail();
            }
        }

        String channel = ContextUtils.get(Constant.REQUEST_CHANNEL);
        // 获取请求渠道枚举对象
        RequestChannelEnum requestChannelEnum = RequestChannelEnum.get(channel);
        if (Objects.isNull(requestChannelEnum)) {
            log.info("密码登录，请求渠道枚举不存在。channel={}", channel);
            return Result.getResult(ResultEnum.ILLEGAL_FAIL.getCode(), "请求渠道不存在");
        }
        registerVo.setRequestChannelEnum(requestChannelEnum);

        LoginDto loginDto = userRegisterManager.registerUser(registerVo);

        log.info("用户注册业务结束");
        return Result.success(loginDto);
    }

}

